package ckw.gui;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

import core.exceptions.OKWWithElectionException;

import ckw.database.DatabaseManager;
import ckw.database.OKW;
import ckw.database.OKWMember;
import ckw.database.Voter;

public class DlgDb extends Dialog {
	private class TblLblPrvdrOKW extends LabelProvider implements
			ITableLabelProvider {
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}

		public String getColumnText(Object element, int columnIndex) {
			OKW okw = (OKW) element;
			return okw.getName() + " - " + okw.getIdentificationField();
		}
	}

	private class TblLblPrvdrOKWMember extends LabelProvider implements
			ITableLabelProvider {
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}

		public String getColumnText(Object element, int columnIndex) {
			OKWMember okwMember = (OKWMember) element;
			return okwMember.getName() + " " + okwMember.getSurname() + " - "
					+ okwMember.getIdentificationField();
		}
	}

	private class TblLblPrvdrVoter extends LabelProvider implements
			ITableLabelProvider {
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}

		public String getColumnText(Object element, int columnIndex) {
			Voter voter = (Voter) element;
			return voter.getName() + " " + voter.getSurname() + " - "
					+ voter.getIdentificationField();
		}
	}

	private TabFolder tabFolder;
	private Dialog dlg;

	private final String strOKW = "OKW";
	private final String strOKWMember = "Czlonek OKW";
	private final String strVoter = "Glosujacy";

	private OKW okw;
	private OKWMember okwMember;
	private Voter voter;

	private DatabaseManager databaseManager;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 */
	public DlgDb(Shell parentShell) {
		super(parentShell);

		databaseManager = DatabaseManager.getInstance();
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		final Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new FormLayout());

		tabFolder = new TabFolder(container, SWT.NONE);
		FormData fd_tabFolder = new FormData();
		fd_tabFolder.left = new FormAttachment(0, 5);
		fd_tabFolder.top = new FormAttachment(0, 5);
		fd_tabFolder.bottom = new FormAttachment(100, -10);
		tabFolder.setLayoutData(fd_tabFolder);

		TabItem tbtmOKW = new TabItem(tabFolder, SWT.NONE);
		tbtmOKW.setText(strOKW);

		Composite cmpOKW = new Composite(tabFolder, SWT.NONE);
		tbtmOKW.setControl(cmpOKW);
		FillLayout fl_cmpOKW = new FillLayout(SWT.HORIZONTAL);
		fl_cmpOKW.marginWidth = 10;
		fl_cmpOKW.marginHeight = 10;
		cmpOKW.setLayout(fl_cmpOKW);

		final TableViewer tblVwrOKW = new TableViewer(cmpOKW, SWT.BORDER
				| SWT.FULL_SELECTION);
		tblVwrOKW.getTable();
		tblVwrOKW.setLabelProvider(new TblLblPrvdrOKW());

		for (OKW okw : databaseManager.getAll(OKW.class)) {
			tblVwrOKW.add(okw);
		}

		TabItem tbtmOKWMember = new TabItem(tabFolder, SWT.NONE);
		tbtmOKWMember.setText(strOKWMember);

		Composite cmpOKWMember = new Composite(tabFolder, SWT.NONE);
		tbtmOKWMember.setControl(cmpOKWMember);
		FillLayout fl_cmpOKWMember = new FillLayout(SWT.HORIZONTAL);
		fl_cmpOKWMember.marginWidth = 10;
		fl_cmpOKWMember.marginHeight = 10;
		cmpOKWMember.setLayout(fl_cmpOKWMember);

		final TableViewer tblVwrOKWMember = new TableViewer(cmpOKWMember,
				SWT.BORDER | SWT.FULL_SELECTION);
		tblVwrOKWMember.getTable();
		tblVwrOKWMember.setLabelProvider(new TblLblPrvdrOKWMember());

		for (OKWMember okwMember : databaseManager.getAll(OKWMember.class)) {
			tblVwrOKWMember.add(okwMember);
		}

		TabItem tbtmVoter = new TabItem(tabFolder, SWT.NONE);
		tbtmVoter.setText(strVoter);

		Composite cmpVoter = new Composite(tabFolder, SWT.NONE);
		tbtmVoter.setControl(cmpVoter);
		FillLayout fl_cmpVoter = new FillLayout(SWT.HORIZONTAL);
		fl_cmpVoter.marginWidth = 10;
		fl_cmpVoter.marginHeight = 10;
		cmpVoter.setLayout(fl_cmpVoter);

		final TableViewer tblVwrVoter = new TableViewer(cmpVoter, SWT.BORDER
				| SWT.FULL_SELECTION);
		tblVwrVoter.getTable();
		tblVwrVoter.setLabelProvider(new TblLblPrvdrVoter());

		for (Voter voter : databaseManager.getAll(Voter.class)) {
			tblVwrVoter.add(voter);
		}

		Composite cmpButtons = new Composite(container, SWT.NONE);
		fd_tabFolder.right = new FormAttachment(100, -61);
		GridLayout gl_cmpButtons = new GridLayout(1, false);
		gl_cmpButtons.marginWidth = 0;
		gl_cmpButtons.marginHeight = 0;
		gl_cmpButtons.horizontalSpacing = 0;
		cmpButtons.setLayout(gl_cmpButtons);
		FormData fd_cmpButtons = new FormData();
		fd_cmpButtons.top = new FormAttachment(0, 29);
		fd_cmpButtons.left = new FormAttachment(tabFolder, 6);
		fd_cmpButtons.right = new FormAttachment(100, -10);
		cmpButtons.setLayoutData(fd_cmpButtons);

		Button btnDodaj = new Button(cmpButtons, SWT.NONE);
		btnDodaj.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				switch (tabFolder.getSelection()[0].getText()) {
				case (strOKW):
					okw = new OKW();
					dlg = new DlgDbOKWAddEdit(container.getShell(), okw, false);
					if (dlg.open() == IDialogConstants.OK_ID) {
						tblVwrOKW.add(okw);
					}
					break;
				case (strOKWMember):
					okwMember = new OKWMember();
					dlg = new DlgDbOKWMemberAddEdit(container.getShell(),
							okwMember, false);
					if (dlg.open() == IDialogConstants.OK_ID) {
						tblVwrOKWMember.add(okwMember);
					}
					break;
				case (strVoter):
					voter = new Voter();
					dlg = new DlgDbVoterAddEdit(container.getShell(), voter,
							false);
					if (dlg.open() == IDialogConstants.OK_ID) {
						tblVwrVoter.add(voter);
					}
					break;
				}
			}
		});
		btnDodaj.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		btnDodaj.setText("Dodaj");

		Button btnEdytuj = new Button(cmpButtons, SWT.NONE);
		btnEdytuj.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				switch (tabFolder.getSelection()[0].getText()) {
				case (strOKW):
					try {
						okw = (OKW) tblVwrOKW.getTable().getSelection()[0]
								.getData();
						dlg = new DlgDbOKWAddEdit(container.getShell(), okw,
								true);
						if (dlg.open() == IDialogConstants.OK_ID) {
							tblVwrOKW.update(okw, null);
						}
					} catch (ArrayIndexOutOfBoundsException e2) {
					}
					break;
				case (strOKWMember):
					try {
						okwMember = (OKWMember) tblVwrOKWMember.getTable()
								.getSelection()[0].getData();
						dlg = new DlgDbOKWMemberAddEdit(container.getShell(),
								okwMember, true);
						if (dlg.open() == IDialogConstants.OK_ID) {
							tblVwrOKWMember.update(okwMember, null);
						}
					} catch (ArrayIndexOutOfBoundsException e2) {
					}
					break;
				case (strVoter):
					try {
						voter = (Voter) tblVwrVoter.getTable().getSelection()[0]
								.getData();
						dlg = new DlgDbVoterAddEdit(container.getShell(),
								voter, true);
						if (dlg.open() == IDialogConstants.OK_ID) {
							tblVwrVoter.update(voter, null);
						}
					} catch (ArrayIndexOutOfBoundsException e2) {
					}
					break;
				}
			}
		});
		btnEdytuj.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnEdytuj.setText("Edytuj");

		Button btnUsun = new Button(cmpButtons, SWT.NONE);
		btnUsun.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				switch (tabFolder.getSelection()[0].getText()) {
				case (strOKW):
					try {
						okw = (OKW) tblVwrOKW.getTable().getSelection()[0]
								.getData();

						int response = SWT.NO;
						OKW okwToBeRemovedFromTable = okw;
						okw = DatabaseManager.getInstance().get(OKW.class, okw.getId());
						if(okw.getOkwMembers().size() > 0) {
							MessageBox messageBox = new MessageBox(container.getShell(),
									SWT.ICON_QUESTION | SWT.YES | SWT.NO | SWT.CANCEL);
							messageBox.setMessage("Wykryto czlonkow przypisanych do tej komisji. Czy chcesz ich usunac?");
							messageBox.setText("Exiting Application");
							response = messageBox.open();
						}
						if(response != SWT.CANCEL) {
							databaseManager.deleteOKW(okw, response == SWT.YES);
							tblVwrOKW.remove(okwToBeRemovedFromTable);
						}

					} catch (ArrayIndexOutOfBoundsException e2) {
					} catch (OKWWithElectionException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					break;
				case (strOKWMember):
					try {
						OKWMember okwMember = (OKWMember) tblVwrOKWMember
								.getTable().getSelection()[0].getData();
						databaseManager.deleteOKWMember(okwMember);
						tblVwrOKWMember.remove(okwMember);
					} catch (ArrayIndexOutOfBoundsException e2) {
					}
					break;
				case (strVoter):
					try {
						Voter voter = (Voter) tblVwrVoter.getTable()
								.getSelection()[0].getData();
						databaseManager.deleteVoter(voter);
						tblVwrVoter.remove(voter);
					} catch (ArrayIndexOutOfBoundsException e2) {
					}
					break;
				}
			}
		});
		btnUsun.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		btnUsun.setText("Usun");

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(482, 348);
	}
}
